package com.ruoyi.doorcontrol.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.doorcontrol.domain.ThDoorControl;
import com.ruoyi.doorcontrol.service.HkUserService;
import com.ruoyi.doorcontrol.service.IThDoorControlService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.doorcontrol.domain.ThDoorcontrolUser;
import com.ruoyi.doorcontrol.service.IThDoorcontrolUserService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 门禁用户Controller
 * 
 * @author zhang
 * @date 2024-07-18
 */
@RestController
@RequestMapping("/doorcontrol/user")
public class ThDoorcontrolUserController extends BaseController
{
    @Autowired
    private IThDoorcontrolUserService thDoorcontrolUserService;
    @Value("${ruoyi.intranet}")
    private String intranet;
    @Autowired
    private HkUserService hkUserService;
    @Autowired
    private IThDoorControlService thDoorControlService;

    /**
     * 查询门禁用户列表
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:user:list')")
    @GetMapping("/list")
    public TableDataInfo list(ThDoorcontrolUser thDoorcontrolUser)
    {
        startPage();
        List<ThDoorcontrolUser> list = thDoorcontrolUserService.selectThDoorcontrolUserList(thDoorcontrolUser);
        return getDataTable(list);
    }

    /**
     * 导出门禁用户列表
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:user:export')")
    @Log(title = "门禁用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ThDoorcontrolUser thDoorcontrolUser)
    {
        List<ThDoorcontrolUser> list = thDoorcontrolUserService.selectThDoorcontrolUserList(thDoorcontrolUser);
        ExcelUtil<ThDoorcontrolUser> util = new ExcelUtil<ThDoorcontrolUser>(ThDoorcontrolUser.class);
        util.exportExcel(response, list, "门禁用户数据");
    }

    /**
     * 获取门禁用户详细信息
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:user:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(thDoorcontrolUserService.selectThDoorcontrolUserById(id));
    }

    /**
     * 新增门禁用户
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:user:add')")
    @Log(title = "门禁用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ThDoorcontrolUser thDoorcontrolUser)
    {
        return toAjax(thDoorcontrolUserService.insertThDoorcontrolUser(thDoorcontrolUser));
    }

    /**
     * 修改门禁用户
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:user:edit')")
    @Log(title = "门禁用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ThDoorcontrolUser thDoorcontrolUser)
    {
        return toAjax(thDoorcontrolUserService.updateThDoorcontrolUser(thDoorcontrolUser));
    }

    /**
     * 删除门禁用户
     */
    @PreAuthorize("@ss.hasPermi('doorcontrol:user:remove')")
    @Log(title = "门禁用户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(thDoorcontrolUserService.deleteThDoorcontrolUserByIds(ids));
    }

    /**
     * 向门禁添加用户
     * @return
     */
    @PostMapping("/controladdUser")
    public AjaxResult addControlUser(@RequestBody ThDoorcontrolUser thDoorcontrolUser){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmss");
        thDoorcontrolUser.setCreateBy(SecurityUtils.getUserId().toString());
        thDoorcontrolUser.setCreateTime(new Date());
        thDoorcontrolUser.setEnable("true");
        String shijian_string = sdf.format(new Date());
        thDoorcontrolUser.setEmployeeNo(shijian_string);
        String photo = thDoorcontrolUser.getPhoto();
        thDoorcontrolUser.setPhoto(intranet+photo);
        ThDoorControl thDoorControl = thDoorControlService.selectThDoorControlById(Long.parseLong(thDoorcontrolUser.getControlIds()));
        AjaxResult ajaxResult = hkUserService.addUser(thDoorControl, thDoorcontrolUser);
        if("200".equals(ajaxResult.get("code"))){
            thDoorcontrolUserService.insertThDoorcontrolUser(thDoorcontrolUser);
        }
        return ajaxResult;
    }
}
